from mcp.server.fastmcp import FastMCP  # 假设这是 MCP 提供的 FastAPI 扩展
import asyncio
import time
from fastapi.responses import StreamingResponse

app = FastMCP()  # 初始化 MCP 服务

@app.get("/mcp/events")
async def sse_stream():
    """MCP 专用 SSE 事件流接口"""
    async def event_generator():
        for i in range(1, 11):
            # 模拟 MCP 设备数据（格式可自定义）
            event_data = {
                "device_id": "mcp-001",
                "timestamp": time.time(),
                "status": "active",
                "data": {"temp": 25 + i, "humidity": 50 + i}
            }
            yield f"data: {event_data}\n\n"
            await asyncio.sleep(1)  # 每秒推送一次

    return StreamingResponse(
        event_generator(),
        media_type="text/event-stream",
        headers={"X-MCP-Version": "1.0"}  # 自定义 MCP 协议头
    )

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)